package priv.mill.init;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import javax.annotation.Resource;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:application.cfg.xml")
public class BootStrap {

	private static Logger logger = Logger.getLogger(BootStrap.class);

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BootStrap.class);
		ZooKeeper bean = context.getBean(ZooKeeper.class);
		try {
			List<ACL> aaa = ZooDefs.Ids.OPEN_ACL_UNSAFE;
			String create = bean.create("/app3", "this is my info".getBytes(), aaa, CreateMode.PERSISTENT);
			System.out.println(create);
			List<String> children = bean.getChildren("/", false);
			System.out.println(children);
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Resource(name="cdl")
	protected CountDownLatch connectedSignal;
	
	private String host="mini1";
	
	@Value("${zkconnectionTimeout}")
	private int sessionTimeoutMs;
	
	@Value("${zkoperationRetryTimeout}")
	private int connectionTimeoutMs;

	@Value("${zkhost}")
	private String zkhost;
	
	@Value("${zkport}")
	private String zkport;

	@Bean
	public ZooKeeper connect() throws IOException, InterruptedException {

		ZooKeeper zoo = new ZooKeeper(host, 5000, new Watcher() {

			public void process(WatchedEvent we) {

				if (we.getState() == KeeperState.SyncConnected) {
					connectedSignal.countDown();
				}
			}
		});

		connectedSignal.await();
		return zoo;
	}
	
	@Bean
	public CuratorFramework curatorFramework() {
		 //RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		        //RetryPolicy retryPolicy = new RetryNTimes(5, 1000);
		        RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000);
//		        CuratorFramework client = CuratorFrameworkFactory
//		                .newClient("192.168.1.105:2181",5000,5000, retryPolicy);
		        
		        CuratorFramework client = CuratorFrameworkFactory
		                .builder()
		                .connectString(zkhost+":"+zkport)
		                .sessionTimeoutMs(sessionTimeoutMs)
		                .connectionTimeoutMs(connectionTimeoutMs)
		                .retryPolicy(retryPolicy)
		                .build();
		        
		        return   client;
	}
}
